package com.base.study.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.SubStringUtil;
import com.base.study.R;
import com.base.study.bean.OnlineStudyListBean;

import java.util.List;

import me.zhouzhuo.zzhorizontalprogressbar.ZzHorizontalProgressBar;

/**
 * Created by zw on 2020/2/27.
 */

public class MyStudyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int HEAD_COUNT = 1;
    private final static int FOOT_COUNT = 0;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    private final static int TYPE_FOOTER = 2;
    private Context context;
    private Activity activity;
    private List<OnlineStudyListBean> list;
    private int scope;
    private String userId;
    private String deptId;

    public MyStudyListAdapter(Context context, Activity activity, List<OnlineStudyListBean> list, int scope, String userId, String deptId) {
        this.context = context;
        this.list = list;
        this.scope = scope;
        this.activity = activity;
        this.userId = userId;
        this.deptId = deptId;
    }

    public int getContentSize() {
        return list.size();
    }

    public boolean isHead(int position) {
        return HEAD_COUNT != 0 && position == 0;
    }

    public boolean isFoot(int position) {
        return FOOT_COUNT != 0 && position == getContentSize() + HEAD_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (HEAD_COUNT != 0 && position == 0) { // 头部
            return TYPE_HEAD;
        } else if (FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize) { // 尾部
            return TYPE_FOOTER;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.header_online_study, parent, false);
            return new MyStudyListAdapter.HeadHolder(itemView);
        } else if (viewType == TYPE_CONTENT) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_my_curriculum, parent, false);
            return new MyStudyListAdapter.ContentHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(context).inflate(R.layout.header_online_study, parent, false);
            return new MyStudyListAdapter.FootHolder(itemView);
        }
    }

    public void setScope(int scope) {
        this.scope = scope;
    }

//    private String ids[] = {
//            "38c0f976112041baaacaf1977f7cb1ee","3aceee33bad7446698b5b493928c3c75",
//            "586d6bd55ebf4a87af13fc19f187ada5","62bd6e3eb6f949e3bd4fc8f47937ad42",
//            "6498509fdc844c14ab56748cdc40faa7","7c7d3248c73742f28243cefa6ffaef0d",
//            "b00f913fc1be41ec85ca3401e5b2d7cf","e65c2c3c80db456db7f2f85520950b03",
//            "f83147231764410bbb7af56d062c9bfa","fdcc2a65ec25432e844e44143e170696"
//    };

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyStudyListAdapter.HeadHolder) { // 头部
            MyStudyListAdapter.HeadHolder headHolder = (MyStudyListAdapter.HeadHolder) holder;
            ((HeadHolder) holder).online_header_scopsse.setText(scope + "");
            /**
             * 跳转积分规则
             */
            headHolder.online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if ("95cf0f4698474b37a1c20af89b0b4217".equals(userId)) {
                        ARouter.getInstance().build("/sweb/common")
                                .withInt("key", 1)
                                .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule02")
                                .withString("title", "江苏中越科技")
                                .withString("name", "测试")
                                .navigation();
                    } else {
                        ARouter.getInstance().build("/sweb/common")
                                .withInt("key", 1)
                                .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
                                .withString("title", "江苏中越科技")
                                .withString("name", "测试")
                                .navigation();
                    }
                }
            });


//            if ("95cf0f4698474b37a1c20af89b0b4217".equals(userId)){
//                for (String s:ids) {
//                    if (!deptId.equals(s)){
//                        headHolder.online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                ARouter.getInstance().build("/sweb/common")
//                                        .withInt("key", 1)
//                                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule02")
//                                        .withString("title", "江苏中越科技")
//                                        .withString("name", "测试")
//                                        .navigation();
//                            }
//                        });
//                    }else {
//                        headHolder.online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                ARouter.getInstance().build("/sweb/common")
//                                        .withInt("key", 1)
//                                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
//                                        .withString("title", "江苏中越科技")
//                                        .withString("name", "测试")
//                                        .navigation();
//                            }
//                        });
//                    }
//
//                }
//            }else {
//                headHolder.online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        ARouter.getInstance().build("/sweb/common")
//                                .withInt("key", 1)
//                                .withString("url",RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
//                                .withString("title", "江苏中越科技")
//                                .withString("name", "测试")
//                                .navigation();
//                    }
//                });
//            }
//


//            headHolder.online_header_scopsse_rule.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ARouter.getInstance().build("/sweb/common")
//                            .withInt("key", 1)
//                            .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRule")
//                            .withString("title", "江苏中越科技")
//                            .withString("name", "测试")
//                            .navigation();
//                }
//            });


            /**
             * 跳转积分排行
             */
            headHolder.online_header_scopsse_ranking.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ARouter.getInstance().build("/sweb/common")
                            .withInt("key", 1)
                            .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/integralRank")
                            .withString("title", "江苏中越科技")
                            .withString("name", "测试")
                            .navigation();
                }
            });
        } else if (holder instanceof MyStudyListAdapter.ContentHolder) { // 内容
            MyStudyListAdapter.ContentHolder myHolder = (MyStudyListAdapter.ContentHolder) holder;
            myHolder.tvName.setText(SubStringUtil.subStringCN(list.get(position - 1).getName(), 30));
            if (TextUtils.isEmpty(list.get(position - 1).getCreateUserName())) {
                myHolder.lin_my_author.setVisibility(View.GONE);
            } else {
                myHolder.lin_my_author.setVisibility(View.VISIBLE);
                myHolder.tvAuthor.setText(list.get(position - 1).getCreateUserName() + "");
            }
            GlideUtil.load(context, list.get(position - 1).getCover(), myHolder.imageView);
            myHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //课程
                    if (list.get(position - 1).getType() == 0) {
                        ARouter.getInstance().build("/curriculum/detail")
                                .withString("id", list.get(position - 1).getId())
                                .withString("url", list.get(position - 1).getCover())
                                .navigation();
                    }
                    if (list.get(position - 1).getType() == 1) {
                        //新闻
                        ARouter.getInstance().build("/home/homenew")
                                .withString("title", list.get(position - 1).getName())
                                .withString("imgurl", list.get(position - 1).getCover())
                                .withString("courseRootId", list.get(position - 1).getFirstCategory())
                                // .withString("detail", list.get(position - 1).getDescription())
                                .withString("id", list.get(position - 1).getId())
                                .navigation();
                    }
                }
            });
        } else { // 尾部

        }
    }

    @Override
    public int getItemCount() {
        return list.size() + HEAD_COUNT + FOOT_COUNT;
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder {
        LinearLayout card_view_four;
        TextView online_header_scopsse;
        TextView online_header_scopsse_rule;
        LinearLayout online_header_scopsse_ranking;

        public HeadHolder(View itemView) {
            super(itemView);
            card_view_four = (LinearLayout) itemView.findViewById(R.id.card_view_four);
            online_header_scopsse = (TextView) itemView.findViewById(R.id.online_header_scopsse);
            online_header_scopsse_rule = (TextView) itemView.findViewById(R.id.online_header_scopsse_rule);
            online_header_scopsse_ranking = (LinearLayout) itemView.findViewById(R.id.online_header_scopsse_ranking);
        }
    }

    // 内容
    private class ContentHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvAuthor;
        ImageView imageView;
        LinearLayout linearLayout;
        LinearLayout lin_my_author;
        ZzHorizontalProgressBar pb;

        public ContentHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.my_cu_name);
            tvAuthor = (TextView) itemView.findViewById(R.id.my_cu_author);
            imageView = (ImageView) itemView.findViewById(R.id.item_image);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.lin_item);
            lin_my_author = (LinearLayout) itemView.findViewById(R.id.lin_my_author);
            pb = (ZzHorizontalProgressBar) itemView.findViewById(R.id.pb);
        }
    }

    // 尾部
    private class FootHolder extends RecyclerView.ViewHolder {
        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
